var express = require('express');
var router = express.Router();

const {
  payMoneyModel,
  videoModel,
  videobackModel,
  video_employeeModel,
  chuangzuojibieModel,
  AppwailianModel,
  tixianModel,
  huiyuandingdanModel,
  xiaoshoueModel,
  PutongyonghuModel,
  chuanbzuozheguanliModel,
  PindaoModel,
  GuanggaoModel,
  GiftModel,
  ChangshipinfenleiModel,
  jubaozhongxinModel,
  tongzhigonggaoModel,
  departmentModel,
  jeuseModel,
  rexiaocihengModel,
} = require('../../../model/model.js');
/* GET home page. */

//内容管理

router.get('/', async (req, res) => {
  res.send({
    code: 200,
  });
});

router.get('/getcontent', async (req, res) => {
  const data = await videobackModel
    .find()
    .populate('type_video')
    .populate('status_user')
    .populate('user_id')
    .populate('shop_type')
    .populate('chuangzuojibie');
  const data2 = await video_employeeModel.find();
  res.send({
    code: 200,
    data,
    data2,
  });
});

router.post('/uploadFile', async (req, res) => {
  const fileContents = req.body;
  let a = JSON.parse(fileContents[0]);
  for (let i = 0; i < a.length; i++) {
    let b = a[i];

    await videobackModel.create({
      title: b.title,
      type_video: b.type_video,
      status: b.status,
      status_user: b.status_user,
      video_path_time: b.video_path_time,
      status_time: b.status_time,
      user_id: b.user_id,
      video_url: b.video_url,
      video_desc: b.video_desc,
      video_fllow: b.video_fllow,
      shop_type: b.shop_type,
    });
  }

  res.send({
    code: 200,
  });
});

router.get('/getchuangzuodengji', async (req, res) => {
  let data = await chuangzuojibieModel.find();
  const data1 = await videobackModel
    .find()
    .populate('type_video')
    .populate('status_user')
    .populate('user_id')
    .populate('shop_type')
    .populate('chuangzuojibie');
  res.send({
    code: 200,
    data,
    data1,
  });
});

router.get('/getwailian', async (req, res) => {
  let data = await AppwailianModel.find();
  res.send({
    code: 200,
    data,
  });
});

router.get('/gettixian', async (req, res) => {
  let data = await tixianModel
    .find()
    .populate('tixian_id')
    .populate('tixian_employee');
  let data1 = await video_employeeModel.find();
  res.send({
    code: 200,
    data,
    data1,
  });
});

router.get('/gethuiyuan', async (req, res) => {
  let data = await huiyuandingdanModel.find().populate('huiyuan_id');
  res.send({
    code: 200,
    data,
  });
});

router.post('/changecontent', async (req, res) => {
  const { body } = req.body;
  await videobackModel.find({ _id: body._id }).updateOne(body);
  res.send({
    code: 200,
    data: '修改成功',
  });
});

const WebSocket = require('ws');
router.post('/websocket', async (req, res) => {
  const wss = new WebSocket.Server({ port: 8080 });
  wss.on('connection', (ws) => {
    console.log('====================================');
    console.log('connection,收到连接');
    console.log('====================================');
  });
  wss.on('message', (ws) => {
    console.log('====================================');
    console.log('message,收到消息' + ws);
    console.log('====================================');
  });
  wss.send('hello world');
  wss.on('close', () => {
    console.log('====================================');
    console.log('close,关闭连接');
    console.log('====================================');
  });
});

router.get('/getxiaoshoue', async (req, res) => {
  let data = await xiaoshoueModel.find();
  res.send({
    code: 200,
    data,
  });
});

router.get('/putongyonghu', async (req, res) => {
  let data = await PutongyonghuModel.find();
  res.send({
    code: 200,
    data,
  });
});

router.post('/deleteputongyonghu', async (req, res) => {
  // console.log(req.body.body.text)
  await PutongyonghuModel.deleteOne({ _id: req.body.body.text });
  res.send({
    code: 200,
    data: 'ok',
  });
});

router.get('/getchuanbzuozheguanli', async (req, res) => {
  let data = await chuanbzuozheguanliModel.find();
  res.send({
    code: 200,
    data,
  });
});

router.post('/deletechuangzuozheguanli', async (req, res) => {
  // console.log(req.body.body.text)
  await chuanbzuozheguanliModel.deleteOne({ _id: req.body.body.text });
  res.send({
    code: 200,
    data: 'ok',
  });
});

router.get('/getpingdao', async (req, res) => {
  let data = await PindaoModel.find();
  res.send({
    code: 200,
    data,
  });
});

router.post('/deletepingdao', async (req, res) => {
  // console.log(req.body.body.text)
  await PindaoModel.deleteOne({ _id: req.body.body.text });
  res.send({
    code: 200,
    data: 'ok',
  });
});

router.get('/getguanggao', async (req, res) => {
  let data = await GuanggaoModel.find();
  res.send({
    code: 200,
    data,
  });
});

router.post('/deleteguanggao', async (req, res) => {
   await GuanggaoModel.deleteOne({ _id: req.body.body.text });
   res.send({
     code: 200,
     data: 'ok',
   });
})

router.get('/getgifts', async (req, res) => {
  let data = await GiftModel.find();
  res.send({
    code: 200,
    data,
  });
})

router.post('/deletegetgifts', async (req, res) => {
  let data = await GiftModel.deleteOne({ _id: req.body.body.text });
  res.send({
    code: 200,
    data,
  });
});

router.get('/get_changshiping', async (req, res) => {
  let data = await ChangshipinfenleiModel.find();
  res.send({
    code: 200,
    data,
  });
})

router.post('/delete_changshiping', async (req, res) => {
  let data = await ChangshipinfenleiModel.deleteOne({
    _id: req.body.body.text,
  });
  res.send({
    code: 200,
  });
});

router.get('/get_jubaozhongxin', async (req, res) => {
  let data = await jubaozhongxinModel.find();
  res.send({
    code: 200,
    data,
  });
})

router.post('/delete_jubaozhongxin', async (req, res) => {
  let data = await jubaozhongxinModel.deleteOne({
    _id: req.body.body.text,
  });
  res.send({
    code: 200,
  
  });
});

router.get('/get_tongzhigonggao', async (req, res) => {
  let data = await tongzhigonggaoModel.find();
  res.send({
    code: 200,
    data,
  });
});

router.post('/delete_tongzhigonggao', async (req, res) => {
  let data = await tongzhigonggaoModel.deleteOne({ _id: req.body.body.text });
  res.send({
    code: 200,
  });
 
});

router.get('/get_department', async (req, res) => {
  let data = await departmentModel.find();
  res.send({
    code: 200,
    data,
  });
});

router.post('/delete_department', async (req, res) => {
  let data = await departmentModel.deleteOne({ _id: req.body.body.text });
  res.send({
    code: 200,
  });

});

router.get('/get_juesebiaoge', async (req, res) => {
  let data = await jeuseModel.find();
  res.send({
    code: 200,
    data,
  });
})


router.post('/delete_juesebiaoge', async (req, res) => {
  let data = await jeuseModel.deleteOne({ _id: req.body.body.text });
  res.send({
    code: 200,
  });
  
});

router.get('/get_rexiaoci', async (req, res) => {
  let data = await rexiaocihengModel.find();
  res.send({
    code: 200,
    data,
  });
})


module.exports = router;
